﻿using Blog.Model;
using Blog.Dto.Article;
using IBaseRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace BaseRepository
{
	public class ArticleRepository:BaseRepository<Article>,IArticleRepository
	{
		public override async Task<List<Article>> QueryAsync() {
			return await Context.Queryable<Article>()
				.Mapper(c => c.ArticleType, c => c.ArticleTypeID, c => c.ArticleType.Id)
				.Mapper(c => c.Author, c => c.AuthorID, c => c.Author.Id)
				.Includes(c => c.Comments)
				.ToListAsync();
		}


		public override async Task<List<Article>> QueryAsync(Expression<Func<Article, bool>> func) {
			return await Context.Queryable<Article>()
				.Where(func)
				.Mapper(c => c.ArticleType, c => c.ArticleTypeID, c => c.ArticleType.Id)
				.Mapper(c => c.Author, c => c.AuthorID, c => c.Author.Id)
				.Includes(c => c.Comments)
				.ToListAsync();
		}


		public async Task<ArticleRelationAuthor> QueryArticleAuthor(Expression<Func<Article,bool>> func)
		{
			return await Context.Queryable<Article>()
				.Where(func)
				.LeftJoin<Author>((x, y) => x.AuthorID == y.Id)
				.Select((x, y) => new ArticleRelationAuthor { Id=x.Id,UserID=y.Id, RoleId=y.RoleId,IsCheck = x.IsCheck})
				.FirstAsync();
		}


		public override async Task<List<Article>> QueryAsync(int PageIndex, int PageSize, RefAsync<int> Total) {
			return await Context.Queryable<Article>()
				.ToPageListAsync(PageIndex, PageSize, Total);
		}

		public override async Task<List<Article>> QueryAsync(Expression<Func<Article, bool>> func,int PageIndex, int PageSize, RefAsync<int> Total)
		{
			return await Context.Queryable<Article>()
				.Where(func)
				.ToPageListAsync(PageIndex, PageSize, Total);
		}
	}
}
